# -*- coding: utf-8 -*-
"""
Created on Fri Oct  7 13:42:57 2011

@author: diavol
"""

import Map
import Item
import Tile
import Creature
import Pic

import pygame, sys, Load

#pygame.display.init()

size = 1200, 700
screen  = pygame.display.set_mode(size)
my_map = Load.load('0.map')

#karta = Map.Map(0, "en tile")
#ruta = Tile.Tile("bild", True, None)
#jag = Creature.Creature("bild", 0, 0, None, 1)

bilder = [Pic.Pic("gubbe_ner.png"), Pic.Pic("gubbe_right.png"), Pic.Pic("gubbe_upp.png"),
          Pic.Pic("gubbe_left.png"), Pic.Pic("gubbe_stop.png")]

pos = 7, 8
speed = 0, 0
gubbe = Creature.Creature(bilder, pos, speed)

def get_input():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
        # keypress events
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
                
            if event.key == pygame.K_DOWN or event.key == pygame.K_s:
                gubbe.speed = 0, 1

            elif event.key == pygame.K_UP or event.key == pygame.K_w:
                gubbe.speed = 0, -1

            elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
                gubbe.speed = -1, 0

            elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                gubbe.speed = 1, 0      
                
        if event.type == pygame.KEYUP:
            gubbe.speed = 0, 0
            
    try:
        tile = my_map.tiles[gubbe.posX+gubbe.speed[0]]\
            [gubbe.posY+gubbe.speed[1]]
        if not tile.okToCross: raise IndexError
        if tile.portalTo: 
            gubbe.posX,gubbe.posY = tile.portalTo.pos
            gubbe.speed = 0,0
    except IndexError:
        gubbe.speed = 0,0

clock = pygame.time.Clock()

while True:
    get_input()
    gubbe.move()
    my_map.draw(screen)
    gubbe.draw(screen)
    
    pygame.display.flip()
    clock.tick(4)
